package id.game;

import id.menu.IOption;
import id.utils.Formulas;
import id.utils.Globals;

import org.fenggui.Button;
import org.fenggui.ComboBox;
import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.Label;
import org.fenggui.RadioButton;
import org.fenggui.TextEditor;
import org.fenggui.ToggableGroup;
import org.fenggui.composite.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.RowExLayout;
import org.fenggui.layout.RowExLayoutData;
import org.fenggui.util.Alignment;
import org.fenggui.util.Spacing;

public class NewGame implements IOption {

	Window frame = null;
	Display desk;
	ComboBox heightList;
	ComboBox widthList;
	TextEditor playerName;
	

	private void buildFrame() {
		frame = FengGUI.createWindow(false, false, false, true);
		frame.setTitle("New Game");

		frame.getContentContainer().setLayoutManager(new RowExLayout(false));
		frame.getContentContainer().getAppearance().setPadding(
				new Spacing(10, 10));

		/**
		 * Grupo dos Radios
		 */

		final ToggableGroup<RadioButton<Integer>> dif_group = new ToggableGroup<RadioButton<Integer>>();
		RowExLayoutData layoutData = new RowExLayoutData(false, false, 1.0d,
				Alignment.LEFT);

		RadioButton<Integer> easy = FengGUI.<Integer> createRadioButton();
		easy.setLayoutData(layoutData);
		easy.setText("Easy");
		easy.setRadioButtonGroup(dif_group);
		easy.setValue(0);
		easy.setSelected(true);

		RadioButton<Integer> medium = FengGUI.<Integer> createRadioButton();
		medium.setLayoutData(layoutData);
		medium.setText("Medium");
		medium.setRadioButtonGroup(dif_group);
		medium.setValue(1);

		RadioButton<Integer> hard = FengGUI.<Integer> createRadioButton();
		hard.setLayoutData(layoutData);
		hard.setText("Hard");
		hard.setRadioButtonGroup(dif_group);
		hard.setValue(2);

		final Label labelDif = FengGUI.createWidget(Label.class);
		labelDif.setLayoutData(new RowExLayoutData(true, true));
		labelDif.setText("Choose the game dificulty:");
		labelDif.getAppearance().setAlignment(Alignment.MIDDLE);
		
		final Label labelName = FengGUI.createWidget(Label.class);
		labelName.setLayoutData(new RowExLayoutData(true, true));
		labelName.setText("Player Name:");
		labelName.getAppearance().setAlignment(Alignment.MIDDLE);

		playerName = FengGUI.createTextEditor(frame.getContentContainer());
		playerName.setEmptyText(" ");
		playerName.setLayoutData(new RowExLayoutData(true, false));
		playerName.setSize(200, 26);
		playerName.setMultiline(false);
		playerName.setWordWarping(false);
		
		final Label boardWidth = FengGUI.createWidget(Label.class);
		boardWidth.setLayoutData(new RowExLayoutData(true, true));
		boardWidth.setText("Choose the board width:");
		boardWidth.getAppearance().setAlignment(Alignment.MIDDLE);

		final Label boardHeight = FengGUI.createWidget(Label.class);
		boardHeight.setLayoutData(new RowExLayoutData(true, true));
		boardHeight.setText("Choose the board height:");
		boardHeight.getAppearance().setAlignment(Alignment.MIDDLE);

		Button start = FengGUI.createWidget(Button.class);
		start.setText("Confirm");
		Button cancel = FengGUI.createWidget(Button.class);
		cancel.setText("Cancel");

		frame.getContentContainer().addWidget(labelName, playerName, labelDif, easy, medium, hard);
		frame.getContentContainer().addWidget(boardWidth);

		Container widthSpacer = FengGUI.createContainer(frame.getContentContainer());
		widthSpacer.getAppearance().setMargin(new Spacing(10, 10, 10, 10));
		widthList = FengGUI.createComboBox(widthSpacer);
		widthList.addItem("3");
		widthList.addItem("4");
		widthList.addItem("5");
		widthList.addItem("6");
		widthList.addItem("7");
		widthList.addItem("8");
		widthList.addItem("9");
		widthList.addItem("10");

		frame.getContentContainer().addWidget(boardHeight);

		Container heightSpacer = FengGUI.createContainer(frame.getContentContainer());
		heightSpacer.getAppearance().setMargin(new Spacing(10, 10, 10, 10));

		heightList = FengGUI.createComboBox(heightSpacer);
		heightList.addItem("3");
		heightList.addItem("4");
		heightList.addItem("5");
		heightList.addItem("6");
		heightList.addItem("7");
		heightList.addItem("8");
		heightList.addItem("9");
		heightList.addItem("10");

		frame.getContentContainer().addWidget(start, cancel);

		labelDif.getAppearance().setMargin(new Spacing(10, 10));

		start.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				if(Globals.getGameData() != null){
					Globals.getGameData().getGrid().close(desk);
				}
				
				GameData gameData = new GameData();
				gameData.setBoardHeight(new Integer(heightList.getSelectedValue()));
				gameData.setBoardWidth(new Integer(widthList.getSelectedValue()));
				gameData.setDificulty(dif_group.getSelectedValue().getValue());
				gameData.generateResources();
				System.out.println("Dificulty Selected :"+ dif_group.getSelectedValue().getValue());
				System.out.println("Width Selected :"+ gameData.getBoardWidth());
				System.out.println("Height Selected :"+ gameData.getBoardHeight());
				
				
				Player player = new Player(playerName.getText(), 
						Formulas.calculateInitialMoney(gameData.getDificulty(), gameData.getBoardWidth(), gameData.getBoardHeight()));
				gameData.setPlayer(player);
				GameGrid gameGrid = new GameGrid();
				gameData.setGrid(gameGrid);
				Globals.setGameData(gameData);
				gameGrid.buildGUI(desk);
				PlayerWindow playerWindow = new PlayerWindow();
				playerWindow.buildGUI(desk);
				frame.close();
				gameData.setPlayerWindows(playerWindow);
				
			}
		});

		cancel.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				frame.close();
			}
		});

		frame.pack();
		desk.addWidget(frame);
	}

	public void buildGUI(Display f) {
		desk = f;
		buildFrame();
	}

	public String getOptionName() {
		return "New Game";
	}

	public String getOptionDescription() {
		return "Start a new game";
	}
	
}
